Data Visualization Using R in Undergraduate Macroeconomics

James M. Murray, Ph.D.

Department of Economics
University of Wisconsin-La Crosse
Email: jmurray@uwlax.edu


St. Louis Fed Professors’ Conference

November 8, 2024

Data Visualization Using R in Undergraduate Macroeconomics

Want to start using the ECODATA R package right now?

Live RStudio project on Posit Cloud!

  1. Login / Create Posit Cloud account at https://posit.cloud

  2. Login / Create FRED account at https://fred.stlouisfed.org/
    (click My Account at top left)

  3. Create your own copy of the ECODATA Exercises project by following:

    https://posit.cloud/content/9084731

  4. Get your FRED key at https://fredaccount.stlouisfed.org/apikeys

  5. Copy that 32-character key and in the Posit Cloud console, set the key:
    ecodata::ecodata_set_fredkey("abcd1234efgh5678ijkl9012mnop3456")

Learn more at https://murraylax.org/ecodata/

Great Recession & Slow Recovery

QR Code

The Good, The Bad, and the Ugly

QR Code

The Good:

  • Student explored data on both FRED and World Bank Data

  • Student picked relevant variables, explained it correctly

  • Cogent international comparisons of the great recession

The Bad:

  • Great interactive tools made for poor visualizations (at least default values)

  • Time consuming to reproduce - Return to website, download image, etc.

The Ugly:

  • Inconsistent style across FRED and World Bank Data

  • Very difficult to see much of the text

QR Code

Introduction

  • Created an R package, ecodata, to facilitate visualizing data in undergraduate macroeconomics courses

  • Wrapper for the fredr, wbstats, and ggplot2 packages

  • Makes it easy to:

    • Download data from the Federal Reserve Economic Data (FRED) and World Bank databases

    • Produce professional-quality data visualizations

    • View descriptions of the data

    • Cite sources

  • Do everything with 3-4 lines of code

QR Code

Example: Fetch the Data

# Load the library
library(ecodata)
library(tidyverse)

# Identify what variables I want
# Includes both FRED and World Bank Data sources
variable_sources <- c(
  "https://fred.stlouisfed.org/series/UNRATE",
  "https://fred.stlouisfed.org/series/AURUKM",
  "https://data.worldbank.org/indicator/FP.CPI.TOTL.ZG?locations=US",
  "https://data.worldbank.org/indicator/FP.CPI.TOTL.ZG?locations=GB"
)

# Fetch the data 
mydata <- get_ecodata(variable_sources)

# Get only Great Recession + Recovery
mydata <- mydata |>
  filter(Date >= "2007-01-01" & Date <= "2016-12-31")

QR Code

Example: Plot the Data

ggplot_ecodata_facet(mydata, ncol = 2, title = "Unemployment and Inflation in U.S. and U.K.") +
  geom_recession()

QR Code

Example: Data Description

ecodata_description_table(mydata)

Variable

Code

Description

Frequency

Units

Seasonal Adjustment

Source

URL

Access Date

Unemployment Rate

UNRATE

Unemployment Rate

Monthly

%

Seasonally Adjusted

FRED (R) Federal Reserve Bank of St. Louis

https://fred.stlouisfed.org/series/UNRATE

October 28, 2024

Unemployment Rate in the United Kingdom

AURUKM

Unemployment Rate in the United Kingdom

Monthly

%

Seasonally Adjusted

FRED (R) Federal Reserve Bank of St. Louis

https://fred.stlouisfed.org/series/AURUKM

October 28, 2024

United States Inflation, consumer prices

FP.CPI.TOTL.ZG?locations=US

Inflation as measured by the consumer price index reflects the annual percentage change in the cost to the average consumer of acquiring a basket of goods and services that may be fixed or changed at specified intervals, such as yearly. The Laspeyres formula is generally used.

Annual

Index

Not Seasonally Adjusted

World Bank Data

https://data.worldbank.org/indicator/FP.CPI.TOTL.ZG?locations=US

October 28, 2024

United Kingdom Inflation, consumer prices

FP.CPI.TOTL.ZG?locations=GB

Inflation as measured by the consumer price index reflects the annual percentage change in the cost to the average consumer of acquiring a basket of goods and services that may be fixed or changed at specified intervals, such as yearly. The Laspeyres formula is generally used.

Annual

Index

Not Seasonally Adjusted

World Bank Data

https://data.worldbank.org/indicator/FP.CPI.TOTL.ZG?locations=GB

October 28, 2024

QR Code

Why Scripting Languages?

  • Reproducible: The code is the set of instructions for what you created

  • Efficient: Difficult to make the first graph, but it’s easy to make the 2nd and 100th graphs!

  • Flexible: Can easily change the data, the graph, the labels, etc.

  • Used in other courses: R is used in econometrics, statistics, data science, etc.

  • Used in industry, even among those who are not data scientists

  • Even more important / relevant with AI

    • AI assistance makes coding more accessible

    • Verification and reproducibility is key with AI-generated content

QR Code

Why Avoid Scripting Languages?

  • Steep learning curve

  • Easy to do the hard things, but hard to do the easy things

  • This is Principles of Macroeconomics, not what I signed up for!

ECODATA Package

  • Gentle learning curve!

  • Easy to do easy things

  • Use simple reproducible code to analyze macroeconomics principles

QR Code

Why ECODATA for Principles of Macroeconomics?

  • Data visualization is a key skill for everyone

  • Early introduction makes subsequent classes easier

  • Data is used throughout the economics discipline, it should be used throughout economics curriculum

  • Repeated practice throughout undergraduate degree builds mastery

  • ECODATA package is easy to learn, easy to use for easy things

QR Code

Best Practices for Principles-Level

  • Show students worked-out examples - See my vignettes!

  • Use prompts that require only 3-4 lines of code

  • Make economics real: Compare theoretical predictions with recent data

Example

  1. Plot the U.S. unemployment rate from 2006-2016

  2. Describe the behavior of the unemployment rate before, during, and years after the recession. What do you notice about economic recoveries?

  3. Plot the U.S. unemployment rate along with Wisconin’s and California’s. What do you notice about the differences in the unemployment rates between these states?

QR Code